<?php

/**
 * This is the model class for table "{{pos_log}}".
 *
 * The followings are the available columns in table '{{pos_log}}':
 * @property integer $zpl_id
 * @property integer $zpo_id
 * @property integer $zpl_content_id
 * @property integer $zpl_start_time
 * @property integer $zpl_end_time
 * @property integer $inputtime
 */
class PosLog extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return PosLog the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{pos_log}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zpo_id, zpl_id, zpl_catid, zpl_content_id, zpl_start_time, zpl_end_time, inputtime', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zpl_id, zpo_id, zpl_content_id, zpl_start_time, zpl_end_time, inputtime', 'safe', 'on'=>'search'),
			array('zpl_des, zpl_title, zpl_pic', 'length', 'max'=>10000),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'product'=>array(self::HAS_ONE, 'Product', array('zp_id'=>'zpl_content_id')),
            'news'=>array(self::HAS_ONE, 'News', array('zne_id'=>'zpl_content_id')),
            'tuan'=>array(self::HAS_ONE, 'ProTuan', array('zpt_id'=>'zpl_content_id')),
            'promotion'=>array(self::HAS_ONE, 'ProTuan', array('zpt_id'=>'zpl_content_id')),
			'discount'=>array(self::HAS_ONE, 'ProDiscount', array('zpd_id'=>'zpl_content_id')),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zpl_id' => 'Zpl',
			'zpo_id' => 'Zpo',
			'zpl_catid' => '分类id',
			'zpl_content_id' => 'Zpl Content',
			'zpl_title' => '标题',
			'zpl_des' => '概要',
			'zpl_pic' => '图片',
			'zpl_start_time' => '开始时间',
			'zpl_end_time' => '结束时间',
			'inputtime' => '创建时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zpl_id',$this->zpl_id);
		$criteria->compare('zpo_id',$this->zpo_id);
		$criteria->compare('zpl_content_id',$this->zpl_content_id);
		$criteria->compare('zpl_start_time',$this->zpl_start_time);
		$criteria->compare('zpl_end_time',$this->zpl_end_time);
		$criteria->compare('inputtime',$this->inputtime);

		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * 根据推荐位id返回内容
	 * @return CActiveDataProvider 
	 */
	public function dataSearch($zpo_id)
	{
		$criteria=new CDbCriteria;

		//$criteria->with = 'product';
		$criteria->order = 'zpl_id DESC';
		$criteria->compare('zpo_id',$zpo_id);
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * 检查是否可以推荐
	 * */
	public function checkForRecommend($zpoid,$contentid,$type)
	{
		$position = Positions::model()->find('zpo_id='.$zpoid.' AND zpo_type='.$type);
		$amount   = PosLog::model()->count('zpo_id='.$zpoid);
		if($type==1){
			$content_exists  = Product::model()->exists('zp_id='.$contentid.' AND zp_status=2 AND zp_state=2');
		}elseif($type==2){
			$content_exists  = News::model()->exists('zne_id='.$contentid.' AND zne_status=2');
		}elseif($type==3){
			$content_exists  = ProTuan::model()->exists('zpt_id='.$contentid.' AND zpt_status=2 AND zpt_type=1 AND zpt_end_time>'.time());
		}elseif($type==4){
			$content_exists  = ProDiscount::model()->exists('zpd_id='.$contentid.' AND zpd_status=1');
		}elseif($type==5){
			$content_exists  = ProTuan::model()->exists('zpt_id='.$contentid.' AND zpt_status=2 AND zpt_type=2 AND zpt_end_time>'.time());
		}else{
			return 6;
		}
		$zpl_exists = PosLog::model()->exists('zpl_content_id='.$contentid.' AND zpo_id='.$zpoid);
		if(!$position){
			return 5;
		}elseif(!$content_exists){
			return 4;
		}elseif($zpl_exists){
			return 3;
		}elseif($amount>=$position->zpo_maxnum){
			return 2;
		}else{
			return 1;
		}
	}
    
	/**
	 * 删除推荐内容
	 * $idbox id数组
	 * $memberid 会员id
	 * */
	public function deleteRecommend($idbox, $memberid=null){
		$condition = 'zpl_id in('.implode(',', $idbox).')';
		if($memberid){
			$condition .= ' and zm_id='.$memberid;
		}
		return $this->deleteAll($condition);
	}

	/**
	 * 保存操作
	 * */
	protected function beforeSave()
	{
		if(parent::beforeSave()){
			if($this->isNewRecord){
				$this->inputtime = time();
			}
			return true;
		}else{
			return false;
		}
	}
}